<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_edit_page');

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After checkperms edit-page');

$THEME->addBreadcrumb($this->adminUrl.'/edit-page/', $this->lang('ap_title_edit-page'));

$VIEW->template = 'admin-edit-page.tpl';

$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/jfx/page-width-slider.js');


if((int) get('page') <= 0){
    $pageid = $DB->oneValue($CONFIG->dbprefix.'pages', 'id', 'is_default = 1');
    JFX::redirect($CONFIG->adminUrl.'/edit-page/?page='.$pageid);
}else{
    $pageid = (int) get('page');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$pageid}'")==0){
        $pageid = $DB->oneValue($CONFIG->dbprefix.'pages', 'id', 'is_default = 1');
    }
}

if($pageid == $SETTINGS->get('template_page_id')){
    JFX::addError($this->lang('cannot_edit_template_page'));
    JFX::redirect($CONFIG->adminUrl);
}

$SMARTY->assign('currentPageId', $pageid);
$SMARTY->assign('currentPageTitle', $LANG->getContent('core', 'heading', $pageid));

JFX::checkAdminPermsRedirect('core', 'perm_page_'.$USER->workspace.'_'.$pageid);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before load page object');

$PAGE = JFX_Block::getBlockById($pageid, $pageid, $USER->workspace);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After load page object');

$SMARTY->assign('PAGE', $PAGE->get());

$pageButtons = $THEME->getPageButtons($pageid);
$SMARTY->assign('pageButtons', $pageButtons);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After get page buttons');

echo '<div class="jfx-droppable jfx-page" id="jfxpage-'.$pageid.'"></div>';

$SMARTY->assign('insertMenu', '<ul class="insert-menu"></ul>');

$SMARTY->assign('canEditPageDetails', true);
$SMARTY->assign('showPageWidthSlider', true);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before hook JFX_Core.editPageLoad');

$hookParams = array('page_id'=>$pageid, 'workspace_id'=>$USER->workspace, 'page'=>&$PAGE);
JFX::hook('JFX_Core.editPageLoad', $hookParams);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After hook JFX_Core.editPageLoad');

$pageAsJson = $PAGE->__toJson();

$VIEW->addJs('var JFX_PAGE_DATA = \''.str_replace("'", "\'", $pageAsJson).'\';');


